﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Text;

namespace MvcApplication1
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }


        /// <summary>
        /// 添加对错误的日志记录
        /// create by:刘刚
        /// create time:2013-02-19
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Error(object sender, EventArgs e)
        {
            #region 日志信息
            Exception ex = this.Context.Server.GetLastError();
            HttpException exhttp = ex as HttpException;
            bool bCheck = true;//HttpException or Exception
            if (exhttp != null) bCheck = false;
            if (ex != null)
            {
                StringBuilder sb = new StringBuilder();
                string logs = string.Empty;
                sb.Append("");
                sb.Append("\r\n----------------------------------------------------------------------------\r\n");
                sb.Append("系统出现如下错误：\r\n");
                sb.Append("　　　　发生时间  ：  " + DateTime.Now.ToString() + "\r\n");
                if (!bCheck)
                {
                    sb.Append("　　　　状态代码  ：  " + exhttp.GetHttpCode().ToString() + "\r\n");
                }
                sb.Append("　　　　错误描述  ：  " + ex.Message.Replace("\r\n", "") + "\r\n");
                sb.Append("　　　　错误对象  ：  " + ex.Source + "\r\n");
                sb.Append("　　　　错误页面  ：  " + HttpContext.Current.Request.Url + "\r\n");
                sb.Append("　　　　用户  IE  ：  " + HttpContext.Current.Request.UserAgent + "\r\n");
                sb.Append("　　　　服务器IP  ：  " + HttpContext.Current.Request.ServerVariables.Get("Local_Addr").ToString() + "\r\n");

                sb.Append("　　　　方法名    ：  " + ex.TargetSite.ToString() + "\r\n");
                sb.Append("　　　　类名      ：  " + ex.TargetSite.DeclaringType.ToString() + "\r\n");
                sb.Append("　　　　成员名    ：  " + ex.TargetSite.MemberType.ToString() + "\r\n");

                sb.Append("　　　　堆栈信息  ：\r\n" + ex.StackTrace + "\r\n");
                sb.Append("\r\n");
                logs = sb.ToString();
                if (bCheck)
                {
                    logs.AddLogFiles("/Logs/Application_Error/" + "Exception.log");
                }
                else if (exhttp.GetHttpCode() >= 500)
                {
                    logs.AddLogFiles("/Logs/Application_Error/" + "Exception.log");
                }

                // Server.ClearError();  //清除异常，其他地方不再捕获此异常。

            }

            #endregion
        }


    }
}